package guoanmaker.operator.business.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import guoanmaker.operator.business.model.PerviderDictionary;
import guoanmaker.operator.business.model.ProvideCommodity;
import guoanmaker.operator.business.model.ReturnResult;
import guoanmaker.operator.business.repository.ProviderDictionaryRepository;


@Service
public class ProviderDictionaryService implements ProviderDictionaryServiceInterface {
	@Autowired
	private  ProviderDictionaryRepository providerDictionaryRepository;
	@PersistenceContext
	private EntityManager manager;
	@Autowired
	private CommodityManageService commodityManageService;

	@Override
	public List<PerviderDictionary> selectdicList(String fatherID) {
		// TODO Auto-generated method stub
		return providerDictionaryRepository.findAllByfatheridList(fatherID);
	}
	
	/**
	 * 根据dictName,fatherID 查询
	 */
	@Override
	public List<PerviderDictionary> selectBydictNameAndfatherID(String dictName, String fatherID) {
		
		return providerDictionaryRepository.selectBydictNameAndfatherID(dictName, fatherID);
	}
	/**
	 * 保存商品单位
	 */
	@Transactional
	@Override
	public ReturnResult savePerviderDictionary(String dictName, String fatherID) {
		ReturnResult returnResult = new ReturnResult();
		if(StringUtils.isNotEmpty(dictName) && StringUtils.isNotEmpty(fatherID)) {
			List<PerviderDictionary> pds = this.selectBydictNameAndfatherID(dictName, fatherID);
			if(pds != null && pds.size() >0) {
				returnResult.setKey("error");
				returnResult.setValue("商品单位已存在");
			} else {
				PerviderDictionary pd = new PerviderDictionary();
				pd.setDictName(dictName);
				pd.setFatherID(fatherID);
				pd.setIsdelete(1);
				providerDictionaryRepository.save(pd);
				returnResult.setKey("success");
				returnResult.setValue("保存成功");
			}
			
		} else {
			returnResult.setKey("error");
			returnResult.setValue("商品单位不能为空");
		}
		
		
		return returnResult;
	}
	
	/**
	 * 根据dictName 模糊查询 PerviderDictionary 集合 并分页
	 */
	@Override
	public Map<String, Object> selectBydictNamePage(String dictName, Integer page, Integer size) {
		String jpql = "select t from PerviderDictionary t where 1 = 1";
		jpql+="and t.fatherID ='100' ";
		if(StringUtils.isNotEmpty(dictName)) {
			jpql += " and t.dictName like '%" + dictName + "%'";
		}
		Query query = manager.createQuery(jpql);
		List<PerviderDictionary> allResult = query.getResultList();
		Map<String, Object> map = new HashMap<>();
		map.put("total", allResult.size());
		query.setFirstResult(page * size);
		query.setMaxResults(size);
		List<PerviderDictionary> result = query.getResultList();
		map.put("PerviderDictionaryList", result);
		
		
		
		return map;
	}
	/**
	 * 根据商品单位id 查询商品中是否有商品正在使用，存在不能删除，不存在 可以删除。
	 */
	@Transactional
	@Override
	public ReturnResult deletePerviderDictionaryById(String id) {
		ReturnResult returnResult = new ReturnResult();
		List<ProvideCommodity> ProvideCommodityList = null;
		ProvideCommodityList = commodityManageService.selectByCommodityStateAndPerviderDictionaryId(id);
		
		if(ProvideCommodityList != null && ProvideCommodityList.size() >0) {
			returnResult.setKey("error");
			returnResult.setValue("正在使用不能删除");
		} else {
			providerDictionaryRepository.delete(id);
			returnResult.setKey("success");
			returnResult.setValue("删除成功");
		}
		return returnResult;
	}


	

}
